package com.dingding.transfer.stamper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dingding.transfer.stamper.constant.Code;
import com.dingding.transfer.stamper.dao.PushRecordDao;
import com.dingding.transfer.stamper.dto.BaseResult;
import com.dingding.transfer.stamper.dto.BaseResultFactory;
import com.dingding.transfer.stamper.dto.ExistApplicationRes;
import com.dingding.transfer.stamper.entity.PushRecord;
import com.dingding.transfer.stamper.handler.DelayMessageSenderFactory;
import com.dingding.transfer.stamper.service.CmdStrategy;
import com.dingding.transfer.stamper.util.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * @author wangxiyuan
 * @description 同步申请单回调 一个设备可以绑定多个申请单，但一个申请单只能对应一台设备
 * @createDate 2021/4/2 10:37
 **/
@Slf4j
public class ExistApplicationResStrategy implements CmdStrategy {
   /* private final ScheduledExecutorService scheduler =
            Executors.newScheduledThreadPool(1);

    class ReportTask implements Runnable{
        private List<UseStampRecord> stampRecords;
        private StampRecordDao stampRecordDao;

        ReportTask(List<UseStampRecord> stampRecords,StampRecordDao stampRecordDao){
            this.stampRecords = stampRecords;
            this.stampRecordDao = stampRecordDao;
        }

        @Override
        public void run() {
            log.info("执行报送任务开始");
            stampRecords.forEach(stampRecord -> {
                stampRecordDao.update(null,new UpdateWrapper<StampRecord>()
                        .eq("serial_id",stampRecord.getSerialId()).set("status",true));

            });
            log.info("执行报送任务结束");
        }
    }*/
    /**
     * 防止重复回调
     * @param object
     * @return
     */
    @Override
    public BaseResult handle(Object object) {
        log.info("同步申请单回调start++++++++++++++++++++++++");
        ExistApplicationRes res = (ExistApplicationRes)object;
        // 非申请单模式 不做处理
        if(StringUtils.isBlank(res.getApplicationId())){
            return BaseResultFactory.produceSuccess();
        }
        PushRecordDao pushRecordDao = SpringContextUtil.getBean(PushRecordDao.class);
        PushRecord pushRecord = pushRecordDao.selectOne(new QueryWrapper<PushRecord>().eq("process_id", res.getApplicationId())
                .eq("status",true));
        if(pushRecord != null){
            // 往钉钉报送 加盖印章记录
            // scheduler.schedule(new ReportTask(stampRecords,stampRecordDao),60000, TimeUnit.MILLISECONDS);
            DelayMessageSenderFactory.DelayMessageSender delayMessageSender = DelayMessageSenderFactory.getInstance();
            delayMessageSender.sendDelayMessage(res.getApplicationId(),60000);
            // 更新使用次数
            boolean update = new PushRecord().setUseCount(res.getUseCount()).setStatus(false).update(new UpdateWrapper<PushRecord>()
                    .eq("uuid", res.getUuid()).eq("process_id",res.getApplicationId()));
            if(!update){
                return BaseResultFactory.produceResult(Code.ERROR,"推送记录更新使用次数失败");
            }
        }

        return BaseResultFactory.produceSuccess();
    }
}
